package hu.myai.indicators;

import hu.myai.api.indicators.AbstractIndicator;
import hu.myai.model.IndicatorVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author bozbalint
 * 
 *         This calculation built on <br>
 *         - moving average(MovingAverage) <br>
 *         - standard deviation (StandardDeviation)<br>
 *         indicators.
 * */
public class LowerBollingerIndicator extends AbstractIndicator {

	/**
	 * The object used when logging debug,errors,warnings and info.
	 * */
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	public LowerBollingerIndicator(Double parameter) {
		super(parameter);
	}

	private String averageID;

	private String deviationID;

	@Override
	public void init() {
		averageID = register(new MovingAverageIndicator(parameter));
		deviationID = register(new StandardDeviationIndicator(parameter));
	}

	@Override
	protected IndicatorVO indicatorCalculation() {

		// I expect MovingAverage registered indicator
		// I expect StandardDeviation registered indicator
		IndicatorVO movIndicator = indicatorMap.get(averageID);
		IndicatorVO sdtDevIndicator = indicatorMap.get(deviationID);

		if (movIndicator == null || sdtDevIndicator == null) {
			return null;
		}

		// calculation of the lowerBowlinger indicator
		double upperBollinger = movIndicator.getValue() - 2 * sdtDevIndicator.getValue();

		IndicatorVO indicator = new IndicatorVO();
		indicator.setValue(upperBollinger);
		indicator.setChanged(true);
		indicator.setDate(sdtDevIndicator.getDate());

		log.debug(indicator.toString());
		return indicator;
	}
}
